import cv2 as cv
from matplotlib import pyplot as plt


def plot_histogram(img):
    plt.hist(img.ravel(), 256, [0, 256])
    plt.show()


def img_histogram(img):
    colors = ['blue', 'green', 'red']
    for i, color in enumerate(colors):
        hist = cv.calcHist([img], [i], None, [256], [0, 256])
        plt.plot(hist, color=color)
        plt.xlim([0, 256])
    plt.show()


src = cv.imread('lena.jpg', cv.IMREAD_COLOR)
cv.namedWindow('demo', cv.WINDOW_AUTOSIZE)
cv.imshow('demo', src)
# plot_histogram(src)
img_histogram(src)
cv.waitKey(0)
cv.destroyWindow('demo')
